package com.weixing.oauth2.common.provider.token;

import com.alibaba.fastjson.JSONObject;
import com.weixing.oauth2.common.userdetails.SecurityUser;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.MacSigner;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.security.jwt.crypto.sign.SignatureVerifier;

/**
 * @Author: kfc
 * @Description: <br/>
 * Date:Create in 2019/6/13 14:51
 * @Modified By:
 */
public class JwtTokenUtil {



    /**
     * token 解码
     */
    public static Jwt decode(String token,String signKey){

       return JwtHelper.decodeAndVerify(token, getVerifier(signKey));
    }

    /**
     * 判断token是否过期
     */
    public static boolean isTokenExpired(String token,String signKey){
        Jwt jwt = JwtHelper.decodeAndVerify(token, getVerifier(signKey));
        String claims = jwt.getClaims();
        JSONObject parse = (JSONObject) JSONObject.parse(claims);
        long exp = parse.getLong("exp");
        return exp*1000 < System.currentTimeMillis();
    }


    /**
     * 判断token是否过期
     */
    public static boolean isTokenExpired(Jwt jwt){
        String claims = jwt.getClaims();
        JSONObject parse = (JSONObject) JSONObject.parse(claims);
        long exp = parse.getLong("exp");
        return exp*1000 < System.currentTimeMillis();
    }

    /**
     * 解析token 获取用户认证信息，放入SpringSecurity上下文中
     *
     * @param token
     */
    public static void pase(String token,String signKey){
        Jwt jwt = JwtHelper.decodeAndVerify(token, getVerifier(signKey));
        String claims = jwt.getClaims();
        System.out.println("claims:" + claims);
        JSONObject parse = (JSONObject) JSONObject.parse(claims);
        String  userName = parse.getString("loginName");
        SecurityUser securityUser = new SecurityUser(userName);
        JwtTokenAuthenticationToken authToken = new JwtTokenAuthenticationToken(securityUser,null);
        SecurityContextHolder.getContext().setAuthentication(authToken);
    }

    /**
     * 解析token 获取用户认证信息，放入SpringSecurity上下文中
     *
     * @param jwt
     */
    public static void pase(Jwt jwt){
        String claims = jwt.getClaims();
        System.out.println("claims:" + claims);
        JSONObject parse = (JSONObject) JSONObject.parse(claims);
        String  userName = parse.getString("loginName");
        SecurityUser securityUser = new SecurityUser(userName);
        JwtTokenAuthenticationToken authToken = new JwtTokenAuthenticationToken(securityUser,null);
        SecurityContextHolder.getContext().setAuthentication(authToken);
    }




    /**
     * 根据key动态判断验签方式
     *
     * @param key
     * @return
     */
    private static SignatureVerifier getVerifier(String key) {
        key = key.trim();
        if (isPublic(key)) {
           return new RsaVerifier(key);
        } else {
            return new MacSigner(key);
        }

    }

    private static boolean isPublic(String key) {
        return key.startsWith("-----BEGIN");
    }


    public static void main(String[] args) {
        String str = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJzaWFzaGFuIiwidWsiOiJZYTh6MXdUT3ZkcDBZY3c0SDd6UFZUcnJhYVBtbUNoSFdGMytYWlBVV1dtK3d2eFVXNUFocmgrRU8vNSs5RU5mNGM0MGZhYTE0NDg5OTkxM2Q4MjlhNTRhOThjNDc0NGYiLCJzY29wZSI6WyInXCIqXCInIl0sInVyb2xlcyI6ImRldmVsb3BlcixkaXJlY3RvcixzZXJ2aWNlIiwiZXhwIjoxNTg1MTk4NDg0LCJ1ciI6InIzIiwianRpIjoiYTI2OWJkOWEtYzY3MC00OWFkLWJhYWQtZDliYmNhMTZiMDk3IiwiY2xpZW50X2lkIjoid2VpeGluZy1tYWxsLWNtcyIsInRpbWVzdGFtcCI6MTU4NTE5MTI4NDk3Mn0.jGYiUY5yZoWktCUfhlgmGjmxPZZQMLtzW9NFPZ1Rzpk";
        Jwt hlghsjy = decode(str, "hlghsjy");
        System.out.println(hlghsjy.getClaims());
    }
}
